package cn.com.captureNetPacket.util;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.com.captureNetPacket.CaptureNetPacketService;
import cn.com.captureNetPacket.model.ConfigData;
import cn.com.captureNetPacket.model.ConfigInfo;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Tools {
	private static  ObjectMapper obojMapper= new ObjectMapper();
	public static DateTimeFormatter dfFull=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
	public static DateTimeFormatter defaultDf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	  // 创建 Pattern 对象  解析sip用户的正则表达式
	public final static Pattern sipUserPattern = Pattern.compile("sip:(.*?)@");
	
	  public static String macAsString(final byte[] mac) {  // 此方法格式化mac地址
		    final StringBuilder buf = new StringBuilder();  
		    if(mac==null ) {
		    	buf.append("no mac");  
		    	return buf.toString();
		    }
		    for (byte b : mac) { 
		      if (buf.length() != 0) {  
		        buf.append(':');  
		      }  
		      if (b >= 0 && b < 16) {  
		        buf.append('0');  
		      }  
		      buf.append(Integer.toHexString((b < 0) ? b + 256 : b).toUpperCase());  
		    }  

		    return buf.toString();  
		  }
	  //int占两个字节的，转换成16进制的字符，然后转数字
	  public static int readInt(final byte[] bs) {
		  final StringBuilder buf = new StringBuilder();  
		  for (byte b : bs) {
			  if (b >= 0 && b < 16) {  
			        buf.append('0');  
			      }  
			      buf.append(Integer.toHexString((b < 0) ? b + 256 : b).toUpperCase());
			      if(buf.length()>2)break;
		}
		  
		return  Integer.valueOf(buf.toString(), 16);  
		  
	  }
	  
	  
 
	  // 读取数据的参数配置
	  
	  /**
	   * 
	  	* <strong>Title: <strong>
	  	* <p>Description: 合并两个数组为新的数组，数组2添加在数组1后面<p>
	  	* <p>Company: </p> 
	  	*<strong>Copyright:</strong> Copyright (c) 2019
	  	* @version: 1.0
	  	* @author  ASUS:augustWei
	  	* @date  2020 上午12:38:41
	  	* @param byte1
	  	* @param byte2
	  	* @return
	   */
	  public static byte[] byteMerger(byte[] byte1, byte[] byte2){
	        byte[] bt3 = new byte[byte1.length+byte2.length];  
	        System.arraycopy(byte1, 0, bt3, 0, byte1.length);  
	        System.arraycopy(byte2, 0, bt3, byte1.length, byte2.length);  
	        return bt3;  
	    } 
	  /**
	   * 
	  	* <strong>Title: <strong>
	  	* <p>Description: 一维数组转为二维数组，剩余部分，copy过去<p>
	  	* <p>Company: </p> 
	  	*<strong>Copyright:</strong> Copyright (c) 2019
	  	* @version: 1.0
	  	* @author  ASUS:augustWei
	  	* @date  2020 上午12:38:41
	  	* @return
	   */
	  public static byte[][] oneDimensional2TwoDimensional(byte[] arraysInt,int rate,int groupCount) {
		  byte[][] secArrays=new byte[groupCount][rate];
			for (int i = 0; i < secArrays.length; i++) {
				 for (int j = 0; j < rate; j++) {
					 secArrays[i][j]=arraysInt[(i)*rate+j];
				}
			}
			return secArrays;
		}
	  
	  public static String  getjsonString(Object obj) {
		  try {
			return obojMapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			 return null;
		}
	  }
	  /**
	   * 获取sip号码
	   * @param sipLine
	   * @return
	   */
	  public static String  getSipUserNumber(String sipLine) {
		  try {
			  // 现在创建 matcher 对象
		      Matcher matcher = sipUserPattern.matcher(sipLine);
		      if(matcher.find()) {
		    	  return matcher.group(1);
		    	  }
		      else return sipLine;
		  } catch (Exception e) {
			 log.error("----------------------------------->>>>{}",sipLine,e);
			  return sipLine;
		  }
	  }
	  
	  
	  
	  
		public static Date localDateTime2Date(LocalDateTime localDateTime) {
			ZoneId zoonid=ZoneId.systemDefault();
			ZonedDateTime zdt=localDateTime.atZone(zoonid);
			return Date.from(zdt.toInstant());
		}
	  
	  
	  
}
